﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Backend.DTO;
using Backend.BLL;

namespace ReSeSSApplication
{
    public partial class Outcomes : System.Web.UI.Page
    {
        protected UserDTO loggedInUserDTO = new UserDTO();
        StudyBLL aStudyBLL = new StudyBLL();
        InclusionOrParameterDTO aInclusionOrParameterDTO = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            AlertError.Visible = false;
            AlertSuccess.Visible = false;
            AlertInfo.Visible = false;

            loggedInUserDTO = (UserDTO)Session["LoggedInUserDTO"];
            RedirectUser(loggedInUserDTO);

            // checking required session for the page
            if (Session["InclusionOrParameterId"] == null)
            {
                Response.Redirect("NewStudy.aspx");
            }
            else
            {
                InclusionOrParameterIdHiddenField.Value = Session["InclusionOrParameterId"].ToString();
                aInclusionOrParameterDTO = aStudyBLL.GetInclusionOrParameter(Convert.ToInt32(InclusionOrParameterIdHiddenField.Value));

                if (aInclusionOrParameterDTO.Type == "Inclusion")
                {
                    EditInclusionHeading.InnerText = "Editing \"" + aInclusionOrParameterDTO.Name + "\" Inclusion";
                }
                else if (aInclusionOrParameterDTO.Type == "Parameter")
                {
                    EditInclusionHeading.InnerText = "Editing \"" + aInclusionOrParameterDTO.Name + "\" Parameter";
                }
                else
                {
                    Response.Redirect("NewStudy.aspx");
                }

                LoadOutcomeGridView();
            }
        }

        protected void AddOutlineButton_Click(object sender, EventArgs e)
        {
            string description = txtDescription.Text.Trim();
            string value = txtValue.Text.Trim();

            if (!string.IsNullOrEmpty(description) && !string.IsNullOrEmpty(value))
            {
                OutcomeDTO aOutcomeDTO = new OutcomeDTO();
                aOutcomeDTO.InclusionOrParameterId = Convert.ToInt32(InclusionOrParameterIdHiddenField.Value);
                aOutcomeDTO.FieldName = description;
                aOutcomeDTO.FieldValue = value;

                aStudyBLL.AddOutcome(aOutcomeDTO);

                AlertSuccess.InnerText = "Outcome added successfully";
                AlertSuccess.Visible = true;
                LoadOutcomeGridView();
                ClearInput();
            }
            else
            {
                AlertError.InnerText = "Both Description and Value are required";
                AlertError.Visible = true;
            }
        }

        protected void OutcomeGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditOutcome")
            {
                try
                {
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    HiddenField outcomeIdHiddenField = (HiddenField)OutcomeGridView.Rows[rowIndex].FindControl("OutcomeId");

                    Session["OutcomeId"] = outcomeIdHiddenField.Value;
                    Response.Redirect("OutcomeDetails.aspx");
                }
                catch (Exception ex)
                {
                    AlertError.InnerText = ex.Message;
                    AlertError.Visible = true;
                }
            }
            else if (e.CommandName == "DeleteOutcome")
            {
                try
                {
                    int rowIndex = Convert.ToInt32(e.CommandArgument);
                    HiddenField outcomeIdHiddenField = (HiddenField)OutcomeGridView.Rows[rowIndex].FindControl("OutcomeId");

                    aStudyBLL.DeleteOutcome(Convert.ToInt32(outcomeIdHiddenField.Value));

                    AlertSuccess.InnerText = "Outcome deleted successfully";
                    AlertSuccess.Visible = true;
                    LoadOutcomeGridView();
                }
                catch (Exception ex)
                {
                    AlertError.InnerText = ex.Message;
                    AlertError.Visible = true;
                }
            }
        }

        protected void OutcomeGridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].ColumnSpan = 2;
                e.Row.Cells.RemoveAt(3);
            }
        }

        private void LoadOutcomeGridView()
        {
            List<OutcomeDTO> listOfOutcome = aStudyBLL.GetOutcomesByInclusionOrParameterId(aInclusionOrParameterDTO.Id);
            OutcomeGridView.DataSource = listOfOutcome;
            OutcomeGridView.DataBind();
        }

        private void ClearInput()
        {
            txtDescription.Text = string.Empty;
            txtValue.Text = string.Empty;
        }

        private void RedirectUser(UserDTO userDTO)
        {
            if (userDTO != null)
            {
                if (userDTO.UserType == "Administrator")
                {
                    Response.Redirect("AdministratorDashboard.aspx");
                }
            }
            else
            {
                Response.Redirect("Index.aspx");
            }
        }
    }
}